home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
c
/
cpptut22.zip
/
CHAP11.TXT
< prev
next >
Wrap
Text File
|
1992-01-20
|
16KB
|
350 lines
Chapter 11
MORE VIRTUAL FUNCTIONS
This chapter will actually be a continuation of the topics covered
in the last chapter but this will be a fuller explanation of what
virtual functions are and how they can be used in a program. We
will present a simple database program with a virtual function to
show how it can be used, then we will go on to illustrate a more
complex use of the virtual function in a manner that finally
illustrates its utility and reason for existence.
HOW TO START AN OOP PROJECT
_________________________________________________________________
The observant student will notice that we begin our use of object
oriented programming by identifying an object, or in this case a
class of objects and even some subordinate objects, which we
completely define. When we get to the main program we then have
a simple job with the remaining needs and they are completed using
standard procedural programming techniques which we are familiar
with. This is the way to begin any object oriented programming
project, by first identifying a few objects that can be separated
conveniently from the rest of the code, programming them, then
writing the main program. It should be added that, for your first
project using objects, do not try to make everything an object.
Select a few objects and after gaining experience with object
oriented programming techniques, use more objects on future
projects. Most programmers use too many objects for their first
project and write very obtuse, unreadable code.
THE PERSON HEADER FILE
_________________________________________________________________
Examine the file named PERSON.H for the ================
definition file for the person class. This PERSON.H
class definition should cause you no problem to ================
understand since there is nothing new here. The
only thing that should be mentioned about this
class is that the protected mode is used for the variables so that
they are readily available in the derived classes which will
inherit this class.
THE PERSON IMPLEMENTATION
_________________________________________________________________
The implementation for the person class is given here and it is a
little strange in the way it is written and used. The intent of
this program is that the virtual method named display() in this
Page 11-1
Chapter 11 - More Virtual Functions
file will never be used, but it is required by ================
the C++ compiler to be used for a default in PERSON.CPP
case some of the subclasses do not have this ================
function available. In the main program we will
be careful to never call this function due to
the nature of the program we are writing. Keep in mind that C++
requires an implementation of all virtual functions even if they
are never used. In this case the message is obviously intended to
be output as an error message.
Be sure to compile this program prior to going on to the next class
definitions.
THE SUPERVISOR HEADER
_________________________________________________________________
The file named SUPERVSR.H contains the class ================
definitions for the three derived classes, SUPERVSR.H
supervisor, programmer, and secretary. These ================
were all placed in a single file for two
reasons. The first reason is to simply
illustrate to you that this can be done, and secondly, to allow
some of the files to be combined on the disk and to require fewer
compilations by you prior to executing the resulting program. This
is actually a good way to combine these files since they are all
derived classes of a common class. It is a matter of style or
personal taste.
You will notice that all three of these classes contain a method
named display() and all have the same return value of void, and all
have the same number of parameters as the parent class's method of
the same name. All of this equality is required because they will
all be called by the same call statement. You will also notice
that the other method in each class has the same name, but
different numbers and types of formal parameters which prevents
this method from being used as a virtual method.
The remainder of this file is simple and you should be able to read
the code and understand it completely. Once again, this file
cannot be compiled or executed.
THE SUPERVISOR IMPLEMENTATION
_________________________________________________________________
The file named SUPERVSR.CPP contains the ================
implementation for the three classes. If you SUPERVSR.CPP
spend a little time studying the code, you will ================
find that each of the methods named init_data()
simply initializes all fields to those passed in
as the actual arguments in a very simple manner.
Page 11-2
Chapter 11 - More Virtual Functions
The method named display(), however, outputs the stored data in
different ways for each class since the data is so different in
each of the classes. Even though the interface to these three
methods is identical, the actual code is significantly different.
There is no reason code besides output could not have been used,
but the output is so visible when the program is executed that it
was chosen for this illustration.
This file should be compiled at this time in preparation for the
next example program which will use all four classes as defined in
these four files.
THE FIRST CALLING PROGRAM
_________________________________________________________________
The file named EMPLOYEE.CPP is the first program ================
that uses the classes developed in this chapter, EMPLOYEE.CPP
and you will find that it is a very simple ================
program.
We begin with an array of ten pointers, each pointing to the base
class. As you recall from the last chapter, this is very important
when using virtual functions, the pointer must point to the base
class. The pointers that will be stored in this array will all
point to objects of the derived classes however. When we use the
resulting pointers to refer to the methods, the system will choose
the method at run time, not at compile time as nearly all of our
other programs have been doing.
We allocate six objects in lines 16 through 39, initialize them to
some values using the methods named init_data(), then assign the
pointers to the members of the array of pointers to person.
Finally, in lines 41 and 42, we call the methods named display()
to display the stored data on the monitor. You will notice that
even though we only use one method call in line 42, we actually
send messages to each of the three methods named display() in the
subclasses. This is true dynamic binding because if we were to
change the values of some of the pointers in the array, we would
then call different methods with the same pointers.
Be sure to compile and execute this program before continuing on
in this chapter. You will recall that the linking step requires
you to combine several files in order to satisfy all system calls.
After you have done that, we will use the same objects in another
way to show how they can be reused.
A PURE VIRTUAL FUNCTION
_________________________________________________________________
The pure virtual function is also available in the C++ toolbox of
possible constructs. You can use a pure virtual function in the
Page 11-3
Chapter 11 - More Virtual Functions
present example program by changing line 10 of PERSON.H to read as
follows;
virtual void display(void) = 0;
You must then eliminate PERSON.CPP from the project or make
sequence. An implementation for a pur